package com.puman.watchtrade.fragment.personal;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ab.view.AbPlayView;
import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.GoodsDetailFragment;
import com.puman.watchtrade.fragment.info.InfoDetailFragment;
import com.puman.watchtrade.fragment.info.adpter.ImgAdapter;
import com.puman.watchtrade.fragment.info.adpter.InfoListViewAdapter;
import com.puman.watchtrade.fragment.info.adpter.MyGallery;
import com.puman.watchtrade.fragment.info.httpHandler.InfoHttpHandler;
import com.puman.watchtrade.fragment.info.model.InfoDatas;
import com.puman.watchtrade.fragment.personal.adapter.SellRecordListViewAdapter;
import com.puman.watchtrade.fragment.personal.handler.PersonalHttpHandler;
import com.puman.watchtrade.fragment.personal.model.BiddRecords;
import com.puman.watchtrade.fragment.personal.model.SellRecords;
import com.puman.watchtrade.util.DataResult;
import com.puman.watchtrade.util.Gjfun;
import com.puman.watchtrade.util.LoadingProgressDialog;
import com.puman.watchtrade.util.PullDownListView;

/*
 */
@SuppressLint({ "ValidFragment", "NewApi" })
public class SellRecordFregment extends Fragment implements OnClickListener,
		PullDownListView.OnRefreshListioner {
	private View mView;

	/*
	 * init UI
	 */
	private ListView listView;
	private TextView toptitle;
	/*
	 * init variable
	 */
	private LayoutInflater inflater;
	private SellRecordListViewAdapter sellRecordListViewAdapter;
	private SellRecords sellRecords = new SellRecords();
	public static final int GET_SUC = 0x1;
	public static final int GET_FAIL = 0x2;
	public static final int END_PRO = 0x3;
	private DataResult dataResult = new DataResult();
	private ArrayList<Integer> imgList;
	private ArrayList<ImageView> portImg;
	private PersonalHttpHandler personalHttpHandler = new PersonalHttpHandler();
	private int pageNumber = 1;
	private int pageNumberTemp = 1;
	private boolean isRefreash = false;
	private LoadingProgressDialog mProgress = null;
	/**
	 * 存储上一个选择项的Index
	 */
	private int preSelImgIndex = 0;

	public SellRecordFregment() {
		super();
	}

	public SellRecordFregment(MainActivity mainActivity) {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.bidding_record_layout, container,
				false);
		this.inflater = inflater;
		initView();
		getInfoList();
		return mView;
	}

	private void initView() {

		toptitle = (TextView) mView.findViewById(R.id.top_title);
		toptitle.setText("出售记录");
		mView.findViewById(R.id.main_left_imgbtn).setOnClickListener(this);
		listView = (ListView) mView.findViewById(R.id.bidding_record_listview);
		sellRecordListViewAdapter = new SellRecordListViewAdapter(
				getActivity(), sellRecords);
		listView.setAdapter(sellRecordListViewAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				FragmentTransaction transaction = MainActivity.fragmentManager
						.beginTransaction();
				GoodsDetailFragment goodsDetailFragment = new GoodsDetailFragment(
						sellRecords.sellRecordList.get(position - 1)
								.getGoodsNo(), sellRecords.sellRecordList.get(
								position - 1).getCompetionStatus());
				transaction.replace(R.id.main_non_bottom, goodsDetailFragment);
				transaction.addToBackStack(null);
				transaction.commit();
			}
		});

		mPullDownView = (PullDownListView) mView.findViewById(R.id.sreach_list);
		mPullDownView.setRefreshListioner(this);
		listView = mPullDownView.mListView;
	}

	private void getInfoList() {
		mProgress = Gjfun.startProgressDialog(mProgress, getActivity());
		((MainActivity) getActivity()).cachedThreadPoolMinPriority
				.execute(infolistRunnable);
	}

	/*
	 */
	Runnable infolistRunnable = new Runnable() {
		@Override
		public void run() {
			int what = GET_FAIL;
			dataResult = personalHttpHandler.sellRecordList(pageNumber + "");
			if (dataResult.flag) {
				what = GET_SUC;
			}
			sendMessage(what);
		}
	};
	PullDownListView mPullDownView;
	Callback callback = new Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case GET_SUC:
				mPullDownView.onRefreshComplete();
				mPullDownView.onLoadMoreComplete();
			
				if (isRefreash) {
					sellRecords.sellRecordList.clear();
				}
				for (int i = 0; i < ((SellRecords) dataResult.object).sellRecordList
						.size(); i++) {
					sellRecords.sellRecordList
							.add(((SellRecords) dataResult.object).sellRecordList
									.get(i));
				}
				
				if (sellRecords.sellRecordList.size() == 0) {
					Gjfun.emptyLayout(mView, "亲，您还没有出售过商品", "", true);
					mView.findViewById(R.id.sreach_list).setVisibility(
							View.GONE);
				} else {
					if (sellRecords.sellRecordList.size() < 6) {
						mPullDownView.setMore(false);
					}else {
						mPullDownView.setMore(true);
					}
					Gjfun.emptyLayout(mView, null, "", false);
					mView.findViewById(R.id.sreach_list).setVisibility(
							View.VISIBLE);
				}
				
				sellRecordListViewAdapter.notifyDataSetChanged();
				Message msg1 = new Message();
				msg1.what = END_PRO;
				handler.sendMessageDelayed(msg1, 1000);
				break;
			case GET_FAIL:
				Gjfun.emptyLayout(mView, "亲，您还没有出售过商品", "", true);
				mView.findViewById(R.id.sreach_list).setVisibility(
						View.GONE);
				Gjfun.stopProgressDialog(mProgress);
				pageNumber--;
				break;
			case END_PRO:
				Gjfun.stopProgressDialog(mProgress);
				break;

			}
			return false;
		}
	};
	Handler handler = new Handler(callback);

	private void sendMessage(int what) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessage(msg);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.main_left_imgbtn:
			MainActivity.getInstant().fragmentManager.popBackStack();
			break;

		default:
			break;
		}

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageNumberTemp = pageNumber;
		pageNumber = 1;
		isRefreash = true;
		getInfoList();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		isRefreash = false;
		pageNumber++;
		getInfoList();
	}

}
